package wnhz.com.business.shareshop.activity.mine;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Message;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import wnhz.com.business.MyApplication;
import wnhz.com.business.R;
import wnhz.com.business.base.BaseRVAdapter;
import wnhz.com.business.base.BaseViewHolder;
import wnhz.com.business.base.MyBaseActivity;
import wnhz.com.business.impl.ActionBarClickListener;
import wnhz.com.business.shareshop.bean.F2TypeGoodsListBean;
import wnhz.com.business.shareshop.bean.MyRebateBean;
import wnhz.com.business.shareshop.ui.TiXianActivityS;
import wnhz.com.business.uitls.MyCallBack;
import wnhz.com.business.uitls.Url;
import wnhz.com.business.uitls.XUtil;
import wnhz.com.business.view.MyRecyclerView;
import wnhz.com.business.view.TranslucentActionBar;

import static com.umeng.socialize.utils.ContextUtil.getContext;

/**
 * 奖励明细
 */
public class DetailRewardsActivity extends MyBaseActivity implements ActionBarClickListener, View.OnClickListener, OnRefreshLoadmoreListener {
    @BindView(R.id.actionbar)
    TranslucentActionBar actionbar;
    @BindView(R.id.empty_layout)
    LinearLayout empty;
    @BindView(R.id.tv_msg)
    TextView tv_msg;
    @BindView(R.id.rewards_detail_refreshLayout)
    SmartRefreshLayout rewards_detail_refreshLayout;
    @BindView(R.id.rewards_detail_myrecyclerview)
    MyRecyclerView rewards_detail_myrecyclerview;
    @BindView(R.id.tv_tixain)
    TextView tv_tixain;

    private BaseRVAdapter mBaseAdapter;

    private int page = 0;
    private List<String> itemdata = new ArrayList<>();

    @Override
    protected void handleMsg(Message msg) {

    }

    public static Intent createIntent(RebateActivity rebateActivity) {
        Intent intent = new Intent(rebateActivity, DetailRewardsActivity.class);
        return intent;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rewards_detail);
        ButterKnife.bind(this);
        actionbar.setData("奖励明细(元)", R.drawable.ic_lefft_white, null, 0, null, 0, this);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            actionbar.setStatusBarHeight(getStatusBarHeight());
        }
        tv_tixain.setOnClickListener(this);
        initView();
    }

    private void initView() {
        rewards_detail_myrecyclerview.setLayoutManager(new LinearLayoutManager(getApplication(), LinearLayoutManager.VERTICAL, false));

        rewards_detail_refreshLayout.setOnRefreshLoadmoreListener(this);
        rewards_detail_refreshLayout.setRefreshHeader(new ClassicsHeader(this)); //getContext() --Fragment
        rewards_detail_refreshLayout.setRefreshFooter(new ClassicsFooter(this)); //getContext() --Fragment
        loadData();
    }

    private void loadData() {
        showDialog("获取中...");
        SharedPreferences sp = getSharedPreferences("user", Context.MODE_PRIVATE);
        String token = sp.getString("token", "");
        Map<String, Object> map = new HashMap<>();
        map.put("token", token);
        map.put("page", page);
        XUtil.Post(Url.Ucenter_rebate, map, new MyCallBack<String>() { //获取我的分享
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                try {
                    Log.e("---我的返利明细:  ", result);
                    JSONObject jsonObject = new JSONObject(result);
                    String re = jsonObject.optString("re");
                    if (re.equals("1")) {
                        Gson gson = new Gson();
                        if (page == 0){
                            if (null != itemdata){
                                itemdata.clear();
                            }
                            rewards_detail_refreshLayout.finishRefresh();
                            rewards_detail_refreshLayout.resetNoMoreData();
                        }else {
                            rewards_detail_refreshLayout.finishLoadmore();
                            mBaseAdapter.notifyDataSetChanged();

                        }
                        initList();
                    }else if ("0".equals(jsonObject.optString("re"))){
                        if (page == 0){
                            rewards_detail_refreshLayout.setVisibility(View.GONE);
                            empty.setVisibility(View.VISIBLE);
                            MyToast(jsonObject.optString("info"));
                            tv_msg.setText("暂无奖励明细");
                        }else {
                            rewards_detail_refreshLayout.finishLoadmore();
                            rewards_detail_refreshLayout.finishLoadmoreWithNoMoreData();//将不会再次触发加载更多事件
                            empty.setVisibility(View.GONE);
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                super.onFinished();
                closeDialog();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                closeDialog();
            }
        });
    }

    private void initList() {
        mBaseAdapter = new BaseRVAdapter(getApplication(), itemdata) {
            @Override
            public int getLayoutId(int viewType) {
                return R.layout.item_rewards_detail;
            }

            @Override
            public void onBind(BaseViewHolder holder, int position) {

            }
        };
        rewards_detail_myrecyclerview.setAdapter(mBaseAdapter);
    }


    @Override
    public void onLeftClick() {
        finish();
    }

    @Override
    public void onRightClick() {

    }

    @Override
    public void onEditTextClick() {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_tixain://提现
                startActivity(new Intent(DetailRewardsActivity.this,TiXianActivityS.class));
                break;
        }
    }

    @Override
    public void onLoadmore(RefreshLayout refreshLayout) {
        rewards_detail_refreshLayout.getLayout().postDelayed(new Runnable() {
            @Override
            public void run() {
                page = page + 1;
                loadData();
            }
        },2000);
    }

    @Override
    public void onRefresh(RefreshLayout refreshLayout) {
        rewards_detail_refreshLayout.getLayout().postDelayed(new Runnable() {
            @Override
            public void run() {
                page = 0;
                loadData();
            }
        },2000);
    }
}
